package com.woniu.tms.studentinfo.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.tms.entity.Classes;
import com.woniu.tms.entity.User;
import com.woniu.tms.studentinfo.service.StudentInfoService;
import com.woniu.tms.studentinfo.service.studentInfoITestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@RequestMapping("student")
@Controller
public class StudentInfoController {
    @Autowired
    StudentInfoService studentService;

    @Autowired
    studentInfoITestService testService;

    // 老师登录 查询他所带班级 学生展示页面
    @RequestMapping("teacherIndex")
    public String teacherIndex() {

        return "WEB-INF/studentinfo/student/studentIndex";

    }

    // 主任登录 查询所有学生 展示页面
    @RequestMapping("supervisorIndex")
    public String supervisorIndex() {

        return "WEB-INF/studentinfo/student/supervisorIndex";

    }

    // 老师登录 查看学员信息分页
    @RequestMapping("teacherIndexDo")
    @ResponseBody
    public Map<String, Object> teacherIndexDo(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, String rnameLike,
                                              HttpSession session) {

        Map<String, Object> map = new HashMap<String, Object>();
        User user = (User) session.getAttribute("loginUser");
        Integer uid = 2;

        try {

            Map<String, Object> map2 = studentService.findByRnameLikeAndUid(rnameLike, uid, pageNum, pageSize);

            List<User> studentList = (List<User>) map2.get("studentList");
            PageInfo<User> pageInfo = (PageInfo<User>) map2.get("pageInfo");

            map.put("msg", true);
            map.put("studentList", studentList);
            map.put("pageInfo", pageInfo);
        } catch (Exception e) {

            map.put("msg", false);
            e.printStackTrace();
        }

        return map;

    }

    // 主任登录 查询所有的学生信息 的分页功能
    @RequestMapping("supervisorIndexDo")
    @ResponseBody
    public Map<String, Object> supervisorIndexDo(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, String rnameLike) {

        Map<String, Object> map = new HashMap<String, Object>();

        try {

            Map<String, Object> map2 = studentService.findByRnameLike(rnameLike, pageNum, pageSize);
            List<User> studentList = (List<User>) map2.get("studentList");
            PageInfo<User> pageInfo = (PageInfo<User>) map2.get("pageInfo");

            map.put("msg", true);
            map.put("studenList", studentList);
            map.put("pageInfo", pageInfo);
        } catch (Exception e) {

            map.put("msg", false);
            e.printStackTrace();
        }

        return map;

    }

    // 删除
    @RequestMapping("teacherDelDo")
    @ResponseBody
    public Map<String, Object> teacherDelDo(Integer[] uid) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            studentService.delete(uid);
            map.put("msg", true);
        } catch (Exception e) {
            map.put("msg", false);
        }

        return map;

    }

    // 学生的添加 跳转页面
    @RequestMapping("teacherAdd")

    public String teacherAdd() {
        return "WEB-INF/studentinfo/student/add";

    }

    // 查询所有的班级 放入下拉列表中
    @RequestMapping("teacherClassList")
    @ResponseBody
    public Map<String, Object> teacherClassList() {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            List<Classes> classList = studentService.findClass();
            map.put("classList", classList);
            map.put("msg", true);
        } catch (Exception e) {
            map.put("msg", false);
            e.printStackTrace();
        }
        return map;

    }

    // 学员的添加
    @RequestMapping("teacherAddDo")
    @ResponseBody
    public Map<String, Object> teacherAddDo(User user) {
        Map<String, Object> map = new HashMap<>();
        try {
            studentService.studentAdd(user);
            studentService.studentEndow(user.getSno());
            map.put("msg", true);
        } catch (Exception e) {
            map.put("msg", false);
            e.printStackTrace();
        }

        return map;

    }

    // 自动生成编号
    @RequestMapping("getCommentCode")
    @ResponseBody
    public Map<String, Object> getCommentCode() {
        Map<String, Object> map = new HashMap<>();
        List<String> bhList = new ArrayList<String>();
        String currentMonth = null;
        int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
        if (month < 10) {
            currentMonth = "0" + month;
        } else {
            currentMonth = month + "";
        }
        List<User> studentList = studentService.findStudent();
        for (User user : studentList) {

            bhList.add(user.getSno());

        }

        String max_code = "";
        String comment_code = "";
        if (bhList.size() > 0) {
            max_code = bhList.get(0);

        }
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String bcid_pfix = format.format(new Date());
        if (max_code != null && max_code.contains(bcid_pfix)) {
            String uid_end = max_code.substring(10);
            int endNum = Integer.parseInt(uid_end);
            int tmpNum = endNum + 1; 
            comment_code = "WN" + bcid_pfix + ("000" + tmpNum);
            map.put("comment_code", comment_code);
        } else {
            comment_code = "WN" + bcid_pfix + "0001";
            map.put("comment_code", comment_code);
        }

        return map;

    }

    // 转班 跳页面


    @RequestMapping("changeClass")
    public String changeClass(Integer uid, Model model) {
        model.addAttribute("uid", uid);

        return "WEB-INF/studentinfo/student/changeClass";

    }

    //审批approval
    @RequestMapping("approval")
    public String approval(Integer uid, Model model) {
        model.addAttribute("uid", uid);

        return "WEB-INF/studentinfo/student/approval";

    }

    // 审批approval
    @RequestMapping("approvals")
    @ResponseBody
    public Map<String, Object> approvals(User user) {
        System.out.println(user.getUid() + "222");
        Map<String, Object> map = new HashMap<>();
        try {
            studentService.updates(user);
            map.put("msg", true);
        } catch (Exception e) {
            map.put("msg", false);
            e.printStackTrace();
        }

        return map;

    }


    // 添加页面 的学生信息的回显
    @RequestMapping("studentList")
    @ResponseBody
    public Map<String, Object> studentList(Integer uid) {
        Map<String, Object> map = new HashMap<>();
        try {
            User user = studentService.findstudent(uid);
            map.put("user", user);
            map.put("msg", true);
        } catch (Exception e) {
            map.put("msg", false);
            e.printStackTrace();
        }

        return map;

    }

    // 转班
    @RequestMapping("changeClassDo")
    @ResponseBody
    public Map<String, Object> changeClassDo(User user) {
        Map<String, Object> map = new HashMap<>();
        try {
            studentService.update(user);
            map.put("msg", true);
        } catch (Exception e) {
            map.put("msg", false);
            e.printStackTrace();
        }

        return map;

    }

    @RequestMapping("imports")
    public String imports() {
        return "WEB-INF/studentinfo/student/import";

    }


    @PostMapping("import")

    public String addUser(@RequestParam("file") MultipartFile file) {


        boolean a = false;
        String fileName = file.getOriginalFilename();
        try {
            a = testService.batchImport(fileName, file);
            return "WEB-INF/studentinfo/student/studentIndex";
        } catch (Exception e) {
            e.printStackTrace();

        }
        return "";
    }


    //班主任 跳转页面
    @RequestMapping("classTeacherIndex")
    public String classTeacherIndex() {

        return "WEB-INF/studentinfo/student/classTeacherIndex";

    }


    // 班主任 分页展示
    @RequestMapping("classTeacherIndexDo")
    @ResponseBody
    public Map<String, Object> classTeacherIndexDo(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, String rnameLike,
                                                   HttpSession session) {

        Map<String, Object> map = new HashMap<String, Object>();
        User user = (User) session.getAttribute("loginUser");
        Integer uid = 5;

        try {

            Map<String, Object> map2 = studentService.findByRnameLikeAndUids(rnameLike, uid, pageNum, pageSize);

            List<User> studentList = (List<User>) map2.get("studentList");
            PageInfo<User> pageInfo = (PageInfo<User>) map2.get("pageInfo");

            map.put("msg", true);
            map.put("studentList", studentList);
            map.put("pageInfo", pageInfo);
        } catch (Exception e) {

            map.put("msg", false);
            e.printStackTrace();
        }

        return map;

    }

}
